package cloudeval.spec

/**
 * The data center specification.
 *
 * @author yfwz100
 */
class DataCenterSpec {

    private Map<String, HostClusterSpec> hostClusterSpecMap = new HashMap<>()
    private Map<String, DataCenterBrokerSpec> vmSpecMap = new HashMap<>()
    private Map<String, WorkloadSpec> workloadSpecMap = new HashMap<>()

    public Map<String, HostClusterSpec> getHostClusterSpecMap() {
        hostClusterSpecMap
    }

    public Map<String, DataCenterBrokerSpec> getVMSpecMap() {
        vmSpecMap
    }

    public Map<String, WorkloadSpec> getWorkloadSpecMap() {
        workloadSpecMap
    }

    /**
     * Define a host cluster.
     *
     * @param name the name of the host cluster case.
     * @param closure the closure to create host clusters.
     * @return the host cluster specification.
     */
    HostClusterSpec HostCluster(String name, @DelegatesTo(HostClusterSpec) Closure closure) {
        def hostClusterSpec = new HostClusterSpec()
        def code = closure.rehydrate(hostClusterSpec, this, this)
        code.resolveStrategy = Closure.DELEGATE_ONLY
        code()
        hostClusterSpecMap[name] = hostClusterSpec
        hostClusterSpec
    }

    /**
     * Define a Broker configuration case.
     *
     * @param name the name of the Broker configuration.
     * @param closure the closure to create Broker configuration.
     * @return the Broker specification.
     */
    DataCenterBrokerSpec Broker(String name, @DelegatesTo(DataCenterBrokerSpec) Closure closure) {
        def vmSpec = new DataCenterBrokerSpec()
        def code = closure.rehydrate(vmSpec, this, this)
        code.resolveStrategy = Closure.DELEGATE_ONLY
        code()
        vmSpecMap[name] = vmSpec
        vmSpec
    }

    /**
     * Define the workload configuration case.
     *
     * @param name the name of the workload configuration.
     * @param closure the closure to create the configuration.
     * @return the workload.
     */
    WorkloadSpec Workload(String name, @DelegatesTo(WorkloadSpec) Closure closure) {
        def workloadSpec = new WorkloadSpec()
        def code = closure.rehydrate(workloadSpec, this, this)
        code.resolveStrategy = Closure.DELEGATE_ONLY
        code()
        workloadSpecMap[name] = workloadSpec
        workloadSpec
    }

}